/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsoft.client.ui;

import ChatSoft.model.User;
import chatsoft.client.buz.ChatBussiness;
import java.io.ObjectOutputStream;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author KID
 */
public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    ObjectOutputStream outStream;
    ChatBussiness chatHandle;
    public User user;

    public MainFrame(ObjectOutputStream outStream, User user) {
        initComponents();
        this.outStream = outStream;
        this.user = user;
        setLocationRelativeTo(this);
        lbHello.setText("Hello: " + user.getUserName());
        chatHandle = new ChatBussiness(this.outStream );
        initFriendList();
    }

    public void initFriendList() {
        treeFriendList.setModel(null);
        DefaultMutableTreeNode[] listNode = new DefaultMutableTreeNode[8];
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Friends List");
        DefaultTreeModel model = new DefaultTreeModel(rootNode);
        DefaultTreeCellRenderer render = (DefaultTreeCellRenderer) treeFriendList.getCellRenderer();
        render.setLeafIcon(new ImageIcon(getClass().getResource("/chatsoft/client/image/online.gif")));
        render.setOpenIcon(new ImageIcon(getClass().getResource("/chatsoft/client/image/group.png")));
        render.setClosedIcon(new ImageIcon(getClass().getResource("/chatsoft/client/image/group.png")));
        for (int i = 0; i < user.getFriendsList().size(); i++) {
            listNode[i] = new DefaultMutableTreeNode(user.getFriendsList().get(i));
            model.insertNodeInto(listNode[i], rootNode, i);
        }
        treeFriendList.setModel(model);
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        treeFriendList = new javax.swing.JTree();
        btnAddFriend = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        txtSearch = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        lbHello = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ChatSoft");

        treeFriendList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                treeFriendListMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(treeFriendList);

        btnAddFriend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/chatsoft/client/image/add_user.png"))); // NOI18N
        btnAddFriend.setText("Add Friend");
        btnAddFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddFriendActionPerformed(evt);
            }
        });

        btnRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/chatsoft/client/image/delete_user.png"))); // NOI18N
        btnRemove.setText("Remove");

        txtSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSearchActionPerformed(evt);
            }
        });

        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/chatsoft/client/image/search_user.png"))); // NOI18N
        btnSearch.setText("search");
        btnSearch.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);

        lbHello.setForeground(new java.awt.Color(255, 0, 0));
        lbHello.setText("lbHello");

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnAddFriend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addComponent(btnRemove))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(btnSearch)))
                    .addComponent(lbHello))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbHello)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddFriend, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSearchActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSearchActionPerformed

    private void btnAddFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFriendActionPerformed
        AddFriendFrame addFriendFrame = new AddFriendFrame(outStream, user);
        addFriendFrame.setVisible(true);
    }//GEN-LAST:event_btnAddFriendActionPerformed

    private void treeFriendListMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeFriendListMousePressed
        String frName;
        try {
        TreePath tPath = treeFriendList.getLeadSelectionPath();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tPath.getLastPathComponent(); //lay ten node
        frName = node.getLastLeaf().toString();
        
            if (node.isLeaf()) {
                chatHandle.sendChatRequest(user,frName);
            }
        } catch (Exception ex) {
        }
    }//GEN-LAST:event_treeFriendListMousePressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddFriend;
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnSearch;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbHello;
    private javax.swing.JTree treeFriendList;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
